package com.chance.cc.crawler.development.command.meta.oauth.xhs;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.development.command.meta.oauth.CrawlerOauthCommand;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lt
 * @version 1.0
 * @date 2021-06-16 15:58:26
 * @email okprog@sina.com
 */
public class XhsUserAuthInfo {

    private static String domain = "xhs";
    private static CrawlerOauthCommand crawlerOauthCommand = new CrawlerOauthCommand("192.168.1.217",9599);

    public static void main(String[] args) throws IOException {
        List<String> authInfos = IOUtils.readLines(new FileInputStream("crawler-scripts-command/src/main/java/com/chance/cc/crawler/development/command/meta/oauth/xhs/xhs-auth-info.txt"), StandardCharsets.UTF_8);
        List<Map<String,String>> allCookieInfoMapList = new ArrayList<>();

        for (String authInfo : authInfos) {
            String[] strings = authInfo.split("\t");
            Map<String,String> auth = new HashMap<>();
            auth.put("authorization",strings[0]);
            auth.put("referer",strings[1]);
            auth.put("device-fingerprint",strings[2]);
            auth.put("openId",strings[3]);
            auth.put("sid",strings[4]);
            auth.put("type",strings[5]);
            if (strings.length <= 7){
                auth.put("flag",strings[6]);
            }else {
                auth.put("flag","ip：" + strings[7] + ";wx: " + strings[6]);
            }

            Map<String,String> domainCookieInfoMap = new HashMap<>();
            domainCookieInfoMap.put("domain",domain);
            domainCookieInfoMap.put("oauthInfo", JSON.toJSONString(auth));

            allCookieInfoMapList.add(domainCookieInfoMap);

        }

        HttpPage httpPage = crawlerOauthCommand.addOrUpdateUserOAuthInfo(allCookieInfoMapList,false);

        System.out.println(httpPage.getRawText());
    }
}
